/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.app.hotel.rest;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.app.hotel.domain.HotelFixedAssets;
import me.zhengjie.modules.app.hotel.service.HotelFixedAssetsService;
import me.zhengjie.modules.app.hotel.service.dto.HotelFixedAssetsDto;
import me.zhengjie.modules.app.hotel.service.dto.HotelFixedAssetsQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import me.zhengjie.modules.app.hotel.service.HotelInfoService;
import me.zhengjie.modules.app.hotel.service.dto.HotelInfoDto;

import java.io.File;
import java.io.IOException;
import java.rmi.ServerException;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

import me.zhengjie.utils.FileUtil;

import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author Haojie Li
* @date 2021-03-28
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "酒店资产管理管理")
@RequestMapping("/api/hotelFixedAssets")
public class HotelFixedAssetsController {

    private final HotelInfoService hotelInfoService;
    private final HotelFixedAssetsService hotelFixedAssetsService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('hotelFixedAssets:list')")
    public void download(HttpServletResponse response, HotelFixedAssetsQueryCriteria criteria) throws IOException {
        hotelFixedAssetsService.download(hotelFixedAssetsService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询酒店资产管理")
    @ApiOperation("查询酒店资产管理")
    @PreAuthorize("@el.check('hotelFixedAssets:list')")
    public ResponseEntity<Object> query(HotelFixedAssetsQueryCriteria criteria, Pageable pageable){

        return new ResponseEntity<>(hotelFixedAssetsService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增酒店资产管理")
    @ApiOperation("新增酒店资产管理")
    @PreAuthorize("@el.check('hotelFixedAssets:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody HotelFixedAssets resources){


        return new ResponseEntity<>(hotelFixedAssetsService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改酒店资产管理")
    @ApiOperation("修改酒店资产管理")
    @PreAuthorize("@el.check('hotelFixedAssets:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody HotelFixedAssets resources){
        hotelFixedAssetsService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除酒店资产管理")
    @ApiOperation("删除酒店资产管理")
    @PreAuthorize("@el.check('hotelFixedAssets:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody String[] ids) {
        hotelFixedAssetsService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("downloadExportTemplate")
    @Log("下载导入模板")
    @ApiOperation("下载导入模板")
    @PreAuthorize("@el.check('hotelFixedAssets:list')")
    public void downloadExportTemplate(HttpServletResponse response) throws IOException{

        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        Map<String,Object> tempObj = new LinkedHashMap<>();
        tempObj.put("序号","");
        tempObj.put("酒店编号","");
        tempObj.put("固定资产号","");
        tempObj.put("固定资产型号","");
        tempObj.put("固定资产名称","");
        tempObj.put("固定资产数量","");
        tempObj.put("备注","");
        tempObj.put("导入状态", "");
        resultList.add(tempObj);
        FileUtil.downloadExcel(resultList, response);
    }

    @PostMapping("export")
    @Log("导入酒店信息")
    @ApiOperation("导入酒店信息")
    @PreAuthorize("@el.check('hotelFixedAssets:list')")
    public void export(@RequestBody Map<String,Object> param, HttpServletResponse response) throws IOException{
        String exportFilePath = (String)param.get("exportFilePath");


        //读取文件内容
        ExcelReader excelReader = ExcelUtil.getReader(new File(exportFilePath));

        List<Map<String, Object>> listAll = excelReader.readAll();
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();

        for(int index = 0; index < listAll.size(); index ++) {
            Map<String,Object> tempObj = listAll.get(index);
            String hotelNo = null != tempObj.get("酒店编号") ? tempObj.get("酒店编号").toString() : "";
            String no = null != tempObj.get("固定资产号") ? tempObj.get("固定资产号").toString() : "";
            String modelNo = null != tempObj.get("固定资产型号") ? tempObj.get("固定资产型号").toString() : "";
            String name = null != tempObj.get("固定资产名称") ? tempObj.get("固定资产名称").toString() : "";
            String num = null != tempObj.get("固定资产数量") ? tempObj.get("固定资产数量").toString() : "0";
            String note = null != tempObj.get("备注") ? tempObj.get("备注").toString() : "";

            //添加酒店房间信息
            tempObj.put("导入状态", "导入成功");
            HotelInfoDto hotelInfoDto = hotelInfoService.findByHotelNo(hotelNo);
            if(hotelInfoDto == null) {
                tempObj.put("导入状态", "导入失败，酒店编号不存在");
                resultList.add(tempObj);
                continue;
            }else {

                if(hotelFixedAssetsService.isExistByModelNo(hotelInfoDto.getId(), no)) {
                    tempObj.put("导入状态", "导入失败，固定资产号已存在");
                    resultList.add(tempObj);
                    continue;
                }

                HotelFixedAssets resources = new HotelFixedAssets();
                resources.setHotelId(hotelInfoDto.getId());
                resources.setHotelName(hotelInfoDto.getHotelName());
                resources.setHotelNo(hotelInfoDto.getHotelNo());
                resources.setNo(no);
                resources.setModelNo(modelNo);
                resources.setName(name);
                resources.setNum(Integer.parseInt(num));
                resources.setNote(note);

                hotelFixedAssetsService.create(resources);
                resultList.add(tempObj);
            }
        }
        FileUtil.downloadExcel(resultList, response);
    }
}
